package cn.chinaflame.pagodas.utils.email;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;


@SuppressWarnings("serial")
@Component
@PropertySource(encoding = "utf-8", value = { "application.properties" })
@ConfigurationProperties(prefix = "mail")
public class Email implements Serializable {
	
	private final static Logger log = LoggerFactory.getLogger(Email.class);
	
	private String host;//发送服务器
	private String name;//发送人名称
	private String sendTo;//收件人邮箱
	private String from;//发件人邮件名
	private String password;//发件人密码
	private String smtpauth;//是否SSL验证
	private String smtpdebug;//邮件是否debug模式
	private int smtponnectiontimeout;//smpt连接超时时间
	private int smtptimeout;//smpt超时时间
	private String encoding;//邮件编码
	private String key;//关键字
	private String copyTo;//抄送人
	private String topic;//邮件标题/主题
	private String body;//邮件正文
	private String sending;//发送中文字提示
	private String sendErr;//发送异常提示
	private String sendSucc;//发送成功提示
	private String[] fileAffix;//附件文件的磁盘地址
	private String userkey;
	private String attachPath;
	private String serverAttachPath;
	
	public String toString() {
		StringBuffer str = new StringBuffer();
		str.append("&host="+host);
		str.append("&name="+name);
		str.append("&sendTo="+sendTo);
		str.append("&from="+from);
		str.append("&password="+password);
		str.append("&smtpauth="+smtpauth);
		str.append("&smtpdebug="+smtpdebug);
		str.append("&smtponnectiontimeout="+smtponnectiontimeout);
		str.append("&smtptimeout="+smtptimeout);
		str.append("&encoding="+encoding);
		str.append("&key="+key);
		str.append("&copyTo="+copyTo);
		str.append("&body="+body);
		str.append("&sending="+sending);
		str.append("&sendErr="+sendErr);
		str.append("&sendSucc="+sendSucc);
		str.append("&userkey="+userkey);
		str.append("&fileAffix="+fileAffix);
		str.append("&attachPath="+attachPath);
		str.append("&serverAttachPath="+serverAttachPath);
		return str.toString();
	}
	
	public Email() {
	}

	/**
	 * @param sendTo 接收人
	 * @param topic 主题
	 * @param body 内容
	 */
	public Email(String sendTo, String topic, String body) {
		this(null, sendTo, null, topic, body, null);
	}

	/**
	 * @param sendTo 接收人
	 * @param topic 主题
	 * @param body 内容
	 * @param fileAffix 附件
	 */
	public Email(String sendTo, String topic, String body, String[] fileAffix) {
		this(sendTo, null, topic, body, fileAffix);
	}

	/**
	 * @param sendTo 接收人
	 * @param copyTo 抄送人
	 * @param topic 主题
	 * @param body 内容
	 */
	public Email(String sendTo, String copyTo, String topic, String body) {
		this(null, sendTo, copyTo, topic, body, null);
	}

	/**
	 * @param sendTo 接收人
	 * @param copyTo 抄送人
	 * @param topic 主题
	 * @param body 内容
	 * @param fileAffix 附件
	 */
	public Email(String sendTo, String copyTo, String topic, String body, String[] fileAffix) {
		this(null, sendTo, copyTo, topic, body, fileAffix);
	}

	/**
	 * @param from 发送人
	 * @param sendTo 接收人
	 * @param copyTo 抄送人
	 * @param topic 主题
	 * @param body 内容
	 */
	public Email(String from, String sendTo, String copyTo, String topic, String body) {
		this(from, sendTo, copyTo, topic, body, null);
	}

	/**
	 * @param from 发送人
	 * @param sendTo 接收人
	 * @param copyTo 抄送人
	 * @param topic 主题
	 * @param body 内容
	 * @param fileAffix 附件
	 */
	public Email(String from, String sendTo, String copyTo, String topic, String body, String[] fileAffix) {
		this(from, null, null, null, sendTo, copyTo, topic, body, fileAffix);
	}

	/**
	 * @param from 发送人
	 * @param name 登录名
	 * @param password 登录密码
	 * @param sendTo 接收人
	 * @param copyTo 抄送人
	 * @param topic 主题
	 * @param body 内容
	 */
	public Email(String from, String name, String password, String key, String sendTo, String copyTo, String topic,
			String body) {
		this(null, from, name, password, key, sendTo, copyTo, topic, body, null);
	}

	/**
	 * @param from 发送人
	 * @param name 登录名
	 * @param password 登录密码
	 * @param sendTo 接收人
	 * @param copyTo 抄送人
	 * @param topic 主题
	 * @param body 内容
	 * @param fileAffix 附件
	 */
	public Email(String from, String name, String password, String key, String sendTo, String copyTo, String topic,
			String body, String[] fileAffix) {
		this(null, from, name, password, key, sendTo, copyTo, topic, body, fileAffix);
	}

	/**
	 * @param host 服务器地址
	 * @param from 发送人
	 * @param name 登录名
	 * @param password 登录密码
	 * @param sendTo 接收人
	 * @param copyTo 抄送人
	 * @param topic 主题
	 * @param body 内容
	 */
	public Email(String host, String from, String name, String password, String key, String sendTo, String copyTo,
			String topic, String body) {
		this(host, from, name, password, key, sendTo, copyTo, topic, body, null);
	}

	/**
	 * @param host 服务器地址
	 * @param from 发送人
	 * @param name 登录名
	 * @param password 登录密码
	 * @param sendTo 接收人
	 * @param copyTo 抄送人
	 * @param topic 主题
	 * @param body 内容
	 * @param fileAffix 附件
	 */
	public Email(String host, String from, String name, String password, String key, String sendTo, String copyTo,
			String topic, String body, String[] fileAffix) {
		this.host = host;
		this.from = from;
		this.name = name;
		this.password = password;
		this.key = key;
		this.sendTo = sendTo;
		this.copyTo = copyTo;
		this.topic = topic;
		this.body = body;
		this.fileAffix = fileAffix;
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public String getFrom() {
//		try {
//			if(StringUtils.isNotBlank(from))
//			return new String(from.getBytes("ISO-8859-1"),"utf-8");
//		} catch (UnsupportedEncodingException e) {
//			log.error(e.getMessage());
//		}
		return from;
	}

	public void setFrom(String from) {
		this.from = from;
	}

	public String getName() {
//		try {
//			if(StringUtils.isNotBlank(name))
//			return new String(name.getBytes("ISO-8859-1"),"utf-8");
//		} catch (UnsupportedEncodingException e) {
//			log.error(e.getMessage());
//		}
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getKey() {
//		try {
//			if(StringUtils.isNotBlank(key))
//			return new String(key.getBytes("ISO-8859-1"),"utf-8");
//		} catch (UnsupportedEncodingException e) {
//			log.error(e.getMessage());
//		}
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getSendTo() {
//		try {
//			if(StringUtils.isNotBlank(sendTo))
//			return new String(sendTo.getBytes("ISO-8859-1"),"utf-8");
//		} catch (UnsupportedEncodingException e) {
//			log.error(e.getMessage());
//		}
		return sendTo;
	}

	public void setSendTo(String sendTo) {
		this.sendTo = sendTo;
	}

	public String getCopyTo() {
//		try {
//			if(StringUtils.isNotBlank(copyTo))
//			return new String(copyTo.getBytes("ISO-8859-1"),"utf-8");
//		} catch (UnsupportedEncodingException e) {
//			log.error(e.getMessage());
//		}
		return copyTo;
	}

	public void setCopyTo(String copyTo) {
		this.copyTo = copyTo;
	}

	public String getTopic() {
//		try {
//			if(StringUtils.isNotBlank(topic))
//			return new String(topic.getBytes("ISO-8859-1"),"utf-8");
//		} catch (UnsupportedEncodingException e) {
//			log.error(e.getMessage());
//		}
		return topic;
	}

	public void setTopic(String topic) {
		this.topic = topic;
	}

	public String getBody() {
//		try {
//			if(StringUtils.isNotBlank(body))
//			return new String(body.getBytes("ISO-8859-1"),"utf-8");
//		} catch (UnsupportedEncodingException e) {
//			log.error(e.getMessage());
//		}
		return body;
	}

	public void setBody(String body) {
		this.body = body;
	}

	public String[] getFileAffix() {
		/*try {
			String[] atta = new String[fileAffix.length] ;
			if(fileAffix !=null && fileAffix.length > 0) {
				for(int i = 0; i <fileAffix.length;i++) {
					String ff =  fileAffix[i];
					if(StringUtils.isNotBlank(ff)) {
						atta[i] = new String(ff.getBytes("ISO-8859-1"),"utf-8");
					}
				}
				return atta;
			}
		} catch (UnsupportedEncodingException e) {
			log.error(e.getMessage());
		}
		return null;*/
		return fileAffix;
	}

	public void setFileAffix(String[] fileAffix) {
		this.fileAffix = fileAffix;
	}

	public String getSmtpauth() {
		return smtpauth;
	}

	public void setSmtpauth(String smtpauth) {
		this.smtpauth = smtpauth;
	}

	public int getSmtponnectiontimeout() {
		return smtponnectiontimeout;
	}

	public void setSmtponnectiontimeout(int smtponnectiontimeout) {
		this.smtponnectiontimeout = smtponnectiontimeout;
	}

	public int getSmtptimeout() {
		return smtptimeout;
	}

	public void setSmtptimeout(int smtptimeout) {
		this.smtptimeout = smtptimeout;
	}

	public String getEncoding() {
		return encoding;
	}

	public void setEncoding(String encoding) {
		this.encoding = encoding;
	}
	

	public String getSmtpdebug() {
		return smtpdebug;
	}

	public void setSmtpdebug(String smtpdebug) {
		this.smtpdebug = smtpdebug;
	}

	public String getSending() {
		/*try {
			if(StringUtils.isNotBlank(sending))
			return new String(sending.getBytes("ISO-8859-1"),"utf-8");
		} catch (UnsupportedEncodingException e) {
			log.error(e.getMessage());
		}
		return null;*/
		return sending;
	}

	public void setSending(String sending) {
		this.sending = sending;
	}

	public String getSendErr() {
	/*	try {
			if(StringUtils.isNotBlank(sendErr))
			return new String(sendErr.getBytes("ISO-8859-1"),"utf-8");
		} catch (UnsupportedEncodingException e) {
			log.error(e.getMessage());
		}
		return null;*/
		return sendErr;
	}

	public void setSendErr(String sendErr) {
		this.sendErr = sendErr;
	}

	public String getSendSucc() {
//		try {
//			if(StringUtils.isNotBlank(sendSucc))
//			return new String(sendSucc.getBytes("ISO-8859-1"),"utf-8");
//		} catch (UnsupportedEncodingException e) {
//			log.error(e.getMessage());
//		}
//		return null;
		return sendSucc;
	}

	public void setSendSucc(String sendSucc) {
		this.sendSucc = sendSucc;
	}

	public String getUserkey() {
		return userkey;
	}

	public void setUserkey(String userkey) {
		this.userkey = userkey;
	}

	public String getAttachPath() {
//		try {
//			if(StringUtils.isNotBlank(attachPath))
//			return new String(attachPath.getBytes("ISO-8859-1"),"utf-8");
//		} catch (UnsupportedEncodingException e) {
//			log.error(e.getMessage());
//		}
		return attachPath;
	}

	public void setAttachPath(String attachPath) {
		this.attachPath = attachPath;
	}

	public String getServerAttachPath() {
//		try {
//			if(StringUtils.isNotBlank(serverAttachPath))
//			return new String(serverAttachPath.getBytes("ISO-8859-1"),"utf-8");
//		} catch (UnsupportedEncodingException e) {
//			log.error(e.getMessage());
//		}
		return serverAttachPath;
	}

	public void setServerAttachPath(String serverAttachPath) {
		this.serverAttachPath = serverAttachPath;
	}
		
}
